/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pedigreereconstruction;

/**
 *
 * @author arthiramachandran
 */
import javax.swing.JFrame;

import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxConstants;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxStylesheet;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import relationships.ProbabilisticRelationship;
import relationships.Relationship;

public class DrawGraph extends JFrame {

    private static final long serialVersionUID = -2707712944901661771L;

    public static void draw(Pedigree p) {
        DrawGraph frame = new DrawGraph(p);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 320);
        frame.setVisible(true);
        //System.out.println("!!");
    }

    public DrawGraph(Pedigree p) {
        super("Hello, World!");

        mxGraph graph = new mxGraph();
        Object parent = graph.getDefaultParent();

        graph.getModel().beginUpdate();
        Map<String, Object> edgeStyle = new HashMap<String, Object>();
//edgeStyle.put(mxConstants.STYLE_EDGE, mxConstants.EDGESTYLE_ORTHOGONAL);
        edgeStyle.put(mxConstants.STYLE_SHAPE, mxConstants.SHAPE_CONNECTOR);
        edgeStyle.put(mxConstants.STYLE_ENDARROW, mxConstants.ARROW_CLASSIC);
        edgeStyle.put(mxConstants.STYLE_LABEL_POSITION, mxConstants.ALIGN_MIDDLE);
        //mxConstants.sty
        edgeStyle.put(mxConstants.STYLE_STROKECOLOR, "#000000");
        //edgeStyle.put(mxConstants.STYLE_FONTCOLOR, "#000000");
        //edgeStyle.put(mxConstants.STYLE_LABEL_BACKGROUNDCOLOR, "#ffffff");

        mxStylesheet stylesheet = new mxStylesheet();
        stylesheet.setDefaultEdgeStyle(edgeStyle);
        graph.setStylesheet(stylesheet);



        HashMap<Individual, Object> list = new HashMap<Individual, Object>();
        HashMap<Individual, Boolean> processedChildren =
                new HashMap<Individual, Boolean>();
        try {

            int x = 50, y = 10;
            for (Individual i : p.getFounders()) {
                if (!list.containsKey(i)) {
                    Object o =
                            graph.insertVertex(parent, null, i.id, x, y, 40, 20);
                    x += 80;
                    list.put(i, o);
                }
                //System.out.println(i);
            }

            /*
            x = 10;
            y += 50;
            for (Individual founder : p.getFounders()) {
            for (Individual child : p.getChildren(founder)) {
            if (!list.containsKey(child)) {
            Object o =
            graph.insertVertex(parent, null, child.id, x, y, 40, 20);
            x += 80;
            y += 10;
            list.put(child, o);
            }
            //graph.insertEdge(parent, null, p.getRelationship(founder, child).relationshipType.toString(), list.get(founder), list.get(child));
            }
            }
            
             * 
             */
            HashMap<Individual, Object> toAdd =
                    new HashMap<Individual, Object>();
            for (Individual ped_parent : list.keySet()) {
                if (processedChildren.containsKey(ped_parent)) {
                    continue;
                }
                x = 50;
                y += 50;

                for (Individual child : p.getChildren(ped_parent)) {
                    if (!list.containsKey(child) && !toAdd.containsKey(child)) {
                        Object o =
                                graph.insertVertex(parent, null, child.id, x, y, 40, 20);
                        x += 80;
                        y += 10;
                        toAdd.put(child, o);
                    }
                }
                //System.out.println("Done: " + ped_parent);
                processedChildren.put(ped_parent, true);
            }
            for (Individual i : toAdd.keySet()) {
                list.put(i, toAdd.get(i));
            }

            for (Iterator<Relationship> r_iterator = p.getRelationshipIterator(); r_iterator.hasNext();) {
                ProbabilisticRelationship r =
                        (ProbabilisticRelationship) r_iterator.next();

                graph.insertEdge(parent, null, r.getRelationshipDistribution().getName(), list.get(r.individual1), list.get(r.individual2));
            }
        } finally {
            graph.getModel().endUpdate();
        }

        mxGraphComponent graphComponent = new mxGraphComponent(graph);
        getContentPane().add(graphComponent);
    }

    public static void main(String[] args) {
        //DrawGraph frame = new DrawGraph();
        //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.setSize(400, 320);
        //frame.setVisible(true);
    }
}